﻿using Com.Lancetinc.Evs.Helpers;
using System;
using System.Windows;
using Com.Lancetinc.Evs.Windows;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// PasswordManageUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class PasswordManageUserControl : ISettingControl
    {
        public PasswordManageUserControl()
        {
            InitializeComponent();
            this.Loaded += PasswordManageUserControl_Loaded;
        }

        private bool _initAutoLogin = false;
        private void PasswordManageUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshAutoLogin();
        }

        private void RefreshAutoLogin()
        {
            _initAutoLogin = LoginHelper.ShouldAutoLogin();
            if (_initAutoLogin)
            {
                ChkAutoLogin.IsChecked = _initAutoLogin;
            }
            else
            {
                ChkAutoLogin.Visibility = Visibility.Collapsed;
            }
        }

        private bool HasInputPassword()
        {
            var oldPassword = CtrlOldPassword.Password.Trim();
            var newPassword = CtrlNewPassword.Password.Trim();
            var newPassword2 = CtrlNewPassword2.Password.Trim();
            return !string.IsNullOrEmpty(oldPassword) ||
                   !string.IsNullOrEmpty(newPassword) ||
                   !string.IsNullOrEmpty(newPassword2);
        }

        private bool ValidPassword()
        {
            var oldPassword = CtrlOldPassword.Password.Trim();
            var newPassword = CtrlNewPassword.Password.Trim();
            var newPassword2 = CtrlNewPassword2.Password.Trim();

            if (oldPassword.Length == 0)
            {
                // 请输入旧密码
                MessageHelper.ShowWarningByMsgId("PasswordManageUserControl.OldPasswordRequired");
                CtrlOldPassword.Focus();
                return false;
            }
            if (newPassword.Length == 0)
            {
                // 请输入新密码
                MessageHelper.ShowWarningByMsgId("PasswordManageUserControl.NewPasswordRequired");
                CtrlNewPassword.Focus();
                return false;
            }
            if (newPassword2.Length == 0)
            {
                // 请输入新密码确认
                MessageHelper.ShowWarningByMsgId("PasswordManageUserControl.NewPasswordConfirmRequired");
                CtrlNewPassword2.Focus();
                return false;
            }
            if (newPassword != newPassword2)
            {
                // 两次输入的新密码不一致
                MessageHelper.ShowWarningByMsgId("PasswordManageUserControl.NewPasswordInconsistent");
                CtrlNewPassword2.Focus();
                return false;
            }

            if (!UserHelper.CheckPassword(oldPassword))
            {
                // 旧密码有误，请重新输入
                MessageHelper.ShowWarningByMsgId("PasswordManageUserControl.OldPasswordWrong");
                CtrlOldPassword.Focus();
                return false;
            }

            return true;
        }

        private bool SavePassword()
        {
            if (!ValidPassword())
                return false;

            var oldPassword = CtrlOldPassword.Password.Trim();
            var newPassword = CtrlNewPassword.Password.Trim();

            try
            {
                UserHelper.ChangePassword(oldPassword, newPassword);
            }
            catch (Exception ex)
            {
                MessageHelper.ShowError(ex.Message);
                return false;
            }
            CtrlOldPassword.Clear();
            CtrlNewPassword.Clear();
            CtrlNewPassword2.Clear();

            return true;
        }

        public bool CheckChanged()
        {
            if (HasInputPassword())
                return true;
            if (ChkAutoLogin.Visibility != Visibility.Visible)
                return false;
            return _initAutoLogin != (ChkAutoLogin.IsChecked == true);
        }

        public bool Save()
        {
            if (!CheckChanged())
                return false;

            if (HasInputPassword())
            {
                if (!SavePassword())
                {
                    return false;
                }
            }

            if (ChkAutoLogin.Visibility == Visibility.Visible && _initAutoLogin != (ChkAutoLogin.IsChecked == true))
            {
                // 取消自动登录
                LoginHelper.SetAutoLogin(false);
                RefreshAutoLogin();
            }

            return true;
        }

        /// <inheritdoc />
        public event EventHandler RequestClose;

        /// <inheritdoc />
        public event EventHandler RequestSave;

        private void BtnClose_OnClick(object sender, RoutedEventArgs e)
        {
            RequestClose?.Invoke(this, EventArgs.Empty);
        }

        private void BtnSave_OnClick(object sender, RoutedEventArgs e)
        {
            RequestSave?.Invoke(this, EventArgs.Empty);
        }
    }
}
